package org.alvin.i18n.ui;

import com.github.houbb.opencc4j.util.ZhConverterUtil;
import com.google.common.collect.Maps;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EasyExcelI18nUtils3 {

    public static void main(String[] args) throws IOException {
        File dir = new File("D:\\tangzhichao\\new_workspace\\quanda\\electrombile\\xiaolancharging\\common\\src\\main\\java\\com\\xl\\common\\exportexcel");
        File[] files = dir.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.toLowerCase().endsWith(".java");
            }
        });

        String regex = "@ExcelProperty\\s?\\(\\s?value\\s?=\\s?\"([^\"]*)\"\\)[\r\n\\s]*private\\s+\\w+\\s+(\\w+)";
        Map<String, LinkedHashMap<String, String>> dict = Maps.newLinkedHashMap();
        for (File file : files) {
            System.out.println(file.getAbsolutePath());
            byte[] buff = Files.readAllBytes(file.toPath());
            String content = new String(buff);
            Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
            Matcher matcher = pattern.matcher(content);
            //
            LinkedHashMap<String, String> words = Maps.newLinkedHashMap();
            while (matcher.find()) {
                // [属性名称=中文]
                words.put(matcher.group(2), matcher.group(1));
            }
            System.out.println(words);
            dict.put(file.getName(), words);
        }
        System.out.println("开始映射");
        //开始做映射
        Map<String, String> zhMap = Maps.newLinkedHashMap();
        Map<String, String> twMap = Maps.newLinkedHashMap();
        for (Map.Entry<String, LinkedHashMap<String, String>> entry : dict.entrySet()) {
            String key = entry.getKey();
            File f = new File(dir, key);
            String fileName = f.getName().substring(0, f.getName().indexOf(".") + 1);
            String content = new String(Files.readAllBytes(f.toPath()));
            for (Map.Entry<String, String> word : entry.getValue().entrySet()) {
                if (word.getValue().contains(fileName.concat("header."))) {
                    System.out.println("已经被转换过：" + word.getValue());
                    continue;
                }
                String tkey = fileName.concat("header.").concat(word.getKey());
                //中文
                zhMap.put(tkey, word.getValue());
                //繁体
                twMap.put(tkey, ZhConverterUtil.toTraditional(word.getValue()));
                //替换内容
                String regexText = "value\\s?=\\s?\"" + Pattern.quote(word.getValue()) + "\"";
                content = content.replaceAll(regexText, "value \\= \"\\{" + tkey + "\\}\"");
            }
            System.out.println(f.getName());
            System.out.println(content);
            Files.write(f.toPath(), content.getBytes(StandardCharsets.UTF_8));
        }
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> line : zhMap.entrySet()) {
            sb.append(line.getKey());
            sb.append("=");
            sb.append(line.getValue());
            sb.append("\n");
        }
        Files.write(Paths.get("D:", "messages_zh_CN.txt"), sb.toString().getBytes(StandardCharsets.UTF_8));


        sb = new StringBuilder();
        for (Map.Entry<String, String> line : twMap.entrySet()) {
            sb.append(line.getKey());
            sb.append("=");
            sb.append(line.getValue());
            sb.append("\n");
        }
        Files.write(Paths.get("D:", "messages_zh_TW.txt"), sb.toString().getBytes(StandardCharsets.UTF_8));
    }
}
